
var btn = document.querySelector(".btn");
var username = document.getElementById("txtUsername");
var password = document.getElementById("txtPassword");
var login_tip = document.getElementById("login_tip");
btn.onclick = async function () {
    console.log(1);
console.log(username.value,password.value);

    var options = {
        url: "http://127.0.0.1:8888/users/login",
        type: "POST",
        data: {
            username: username.value,
            password: password.value
        },
        dataType: "json"
    }
    $.ajax(options)
        .done(function (data) {
            // 用switch定义code情况; 
            switch (data.code) {
                case 0:
                    errorTip(data.message);
                    break;
                case 1:
                    successTip(data.message);
                    setTimeout(function () {

                        if (location.hash === "#goods") {
                            history.go(-1);
                        } else {
                            location.href = "ddindex.html"
                        }
                    }, 2000)
                    // 登录成功之后获取存储用的id; 
                    setCookie("userid", data.user.id);
                    setCookie("token", data.token, {
                        expires: 1
                    });
                    break;
            }
        })

}

function errorTip(msg) {
    login_tip.style.display = "block";
    login_tip.classList.remove("alert-success");
    login_tip.classList.add("alert-danger");

    login_tip.innerHTML = msg;

    setTimeout(function () {
        login_tip.style.display = "none";
    }, 2000)
}
function successTip(msg) {
    login_tip.classList.remove("alert-danger");
    login_tip.classList.add("alert-success");
    login_tip.style.display = "block";
    login_tip.innerHTML = msg;

    setTimeout(function () {
        login_tip.style.display = "none";
    }, 2000)
}